package com.wanmait.schedule.controller;

import com.wanmait.schedule.entity.Classes;
import com.wanmait.schedule.entity.Teacher;
import com.wanmait.schedule.service.ClassesService;
import com.wanmait.schedule.service.TeacherService;
import com.wanmait.schedule.util.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 班级表 前端控制器
 * </p>
 *
 * @author wanmait
 * @since 2022-12-05
 */
@RestController
@RequestMapping("/manage/classes")
@RequiresPermissions("manage:classes")
public class ClassesController {
    @Resource
    private ClassesService classesService;

    @Resource
    private TeacherService teacherService;

    @GetMapping("listClasses")
    public AjaxResult listClasses(){
        return AjaxResult.success(classesService.list());
    }

    //班级列表
    @PostMapping("list")
    public AjaxResult list(@RequestBody Classes classes,Integer pageNum){
        return AjaxResult.success(classesService.list(classes,pageNum));
    }


    //获得教师
    @GetMapping("/findTeacher")
    public AjaxResult findTeacherList(){
        return AjaxResult.success("成功").put("teacherList",teacherService.list());
    }

    //班级列表
    @GetMapping("list")
    public AjaxResult list(){
        return AjaxResult.success(classesService.list());
    }

    //新增班级 修改班级
    @PostMapping("save")
    public AjaxResult save(@RequestBody Classes classes, @RequestAttribute Teacher teacher){
        if(classes.getId()==null){
            List<Classes> classesList = classesService.list();
            for (Classes i: classesList){
                if(i.getClassesName().equals(classes.getClassesName())){
                    return AjaxResult.error(10001,"班级名已存在");
                }
            }
            classes.setInputId(teacher.getId());
            classesService.save(classes);
        }else {
            classesService.updateById(classes);
        }
        return AjaxResult.success("保存成功",classes);
    }

    //删除班级
    @GetMapping("delete")
    public AjaxResult delete(Integer id){
        classesService.removeById(id);
        return AjaxResult.success("删除成功");
    }

    //根据id获得班级信息
    @GetMapping("findById")
    public AjaxResult findById(Integer id){
        Classes classes = classesService.findById(id);
        return AjaxResult.success("查询成功",classes);
    }

    //总条数
    @GetMapping("total")
    public AjaxResult total(){
        return AjaxResult.success("成功",classesService.count());
    }
}
